﻿using System;
using System.IO;

namespace ServerCrafter.ClassLibrary
{
    public static class Logger
    {
        private static readonly string LogFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                        @"\ServerCrafter\.log";

        private static StreamWriter logger;

        private static void Write(string msg)
        {
            var dateTimeStamp = DateTime.Now.ToString("[yyyy.MM.dd][hh:mm:ss.fff tt]");
            logger.WriteLine(dateTimeStamp + msg);
        }

        public static void WriteInitialization()
        {
            try
            {
                logger = File.AppendText(LogFile);
            }
            catch (PathTooLongException exception)
            {
                // The specified path, file name, or both exceed the system-defined maximum length. For example,
                // on Windows-based platforms, paths must be less than 248 characters, and file names must be less
                // than 260 characters.
            }
            catch (DirectoryNotFoundException exception)
            {
                // The specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
            }
            catch (Exception exception)
            {
                // Some unknown exception has occured
            }

            WriteInfo("ServerCrafter Logger has started initializating.");
            // Add client computer information
            WriteInfo("ServerCrafter Logger has finished initializating.");
        }

        public static void WriteInfo(string msg)
        {
            Write(" [INFO] " + msg);
        }

        public static void WriteWarning(string msg)
        {
            Write(" [WARNING] " + msg);
        }

        public static void WriteSevere(string msg)
        {
            Write(" [SEVERE] " + msg);
        }

        public static void Dispose()
        {
            logger.Close();
            logger.Dispose();
        }
    }
}